package com.library.books.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.library.books.domain.Student;
import com.library.books.domain.vo.borrow.BooksJoinBorrowRecord;
import com.library.books.domain.vo.borrow.StudentBorrowVo;
import com.library.books.mapper.StudentMapper;
import com.library.books.service.IBooksBorrowService;
import com.library.books.service.IStudentService;
import com.library.common.constant.SysConfigConstants;
import com.library.common.core.domain.entity.SysDictType;
import com.library.common.utils.spring.SpringUtils;
import com.library.system.service.ISysConfigService;
import com.library.system.service.ISysDictDataService;
import com.library.system.service.ISysDictTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 学生信息Service业务层处理
 *
 * @author hb
 * @date 2024-10-14
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService
{

    @Resource
    IBooksBorrowService booksBorrowService;

    @Resource
    ISysConfigService configService;

    /**
     * 查询学生信息列表
     *
     *
     * @param student 学生信息
     * @return 学生信息
     */
    @Override
    public List<Student> selectStudentList(Student student) {
        return baseMapper.selectStudentList(student);
    }

    /**
     * 根据学号查询学生信息
     *
     *
     * @param cardNumber 卡号
     * @return 学生信息
     */
    @Override
    public StudentBorrowVo selectStudentInfoByCardNumber(String cardNumber) {
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("student_card_number",cardNumber);
        Student student = baseMapper.selectOne(studentQueryWrapper);
        if (student != null){
            StudentBorrowVo studentBorrowVo = new StudentBorrowVo();
            BeanUtils.copyProperties(student,studentBorrowVo);

            // 查询当前逾期次数
            BooksJoinBorrowRecord booksJoinBorrowRecord = new BooksJoinBorrowRecord();
            booksJoinBorrowRecord.setBorrowCard(student.getStudentCardNumber());
            studentBorrowVo.setOverdueCount(booksBorrowService.selectBooksBorrowBeOverdueCount(booksJoinBorrowRecord));
            studentBorrowVo.setOverdue(studentBorrowVo.getOverdueCount() > 0);
            // 查询当前借阅次数
            studentBorrowVo.setLendCount(booksBorrowService.selectBooksBorrowBeLendCount(booksJoinBorrowRecord));
            // 系统参数 每个人最大借阅数量(当前总借阅数量)
            String lendMax = configService.selectConfigByKey(SysConfigConstants.MAX_LEND_CONFIG_KEY);
            studentBorrowVo.setLendOnlyMaxNum(lendMax == null?5:Long.parseLong(lendMax));
            return studentBorrowVo;
        }
        throw new RuntimeException("未查询到该学生信息");
    }

    /**
     * 根据学号查询学生基本信息
     *
     *
     * @param cardNumber 卡号
     * @return 学生信息
     */
    @Override
    public Student selectStudentBasicInfoByCardNumber(String cardNumber) {
        Student student = baseMapper.selectOne(Wrappers.lambdaQuery(Student.class)
            .select(Student::getStudentName,
                    Student::getStudentNumber,
                    Student::getStudentCardNumber,
                    Student::getStudentClass,
                    Student::getStudentHeadPic
            )
            .eq(Student::getStudentCardNumber,cardNumber)
        );
        if (student != null) {
            return student;
        }
        throw new RuntimeException("未查询到该学生信息");
    }
}
